package com.monlyu.util.convertor;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.monlyu.util.MathUtil;
import com.monlyu.util.StringUtil;


/**
 * @since Fan Houjun 2009-2-8
 */
public class BigDecimalConverter extends StrutsTypeConverter {

	public Object convertFromString(Map context, String[] values, Class toClass) {
		if (values != null && values.length > 0) {
			if (StringUtil.isEmpty(values[0])) {
				return null;
			}
			return new BigDecimal(values[0].replaceAll(",", "").trim());
		}
		return null;
	}

	public String convertToString(Map context, Object obj) {
		if (obj instanceof BigDecimal) {
			return MathUtil.format(obj);
		} else {
			return "0.00";
		}
	}
	public static void main(String[] args) {
		final String str = MathUtil.format(new BigDecimal("-0.0000123"));
		System.out.println("-0.00".equals(str) ? "0.00" : str);
	}
}
